<?php

class Joob_Controller_Front {
	
	protected $dispatcher;
	
	protected $request;
	
	/**
	 * @return the $request
	 */
	public function getRequest() {
		return $this->request;
	}

	/**
	 * @param field_type $request
	 */
	public function setRequest($request) {
		$this->request = $request;
	}

	/**
	 * Change the current dispatcher
	 * @param unknown_type $dispatcher
	 */
	public function setDispatcher($dispatcher)	{
		$this->dispatcher = $dispatcher;
	}
	
	/**
	 * Get the current dispatcher
	 */
	public function getDispatcher()	{
		return $this->dispatcher;
	}

	/**
	 * Dispatch the current request
	 * @param unknown_type $request
	 * @param unknown_type $response
	 */
	public function dispatch($request = null, $response = null)	{
		$dispatcher = $this->getDispatcher();
		
		if ($request == null)	{
			require_once 'Joob/Http/Request/Http.php';
			$request = new Joob_Http_Request_Http();
		}
		
		$this->setRequest($request);
		
		if ($response == null)	{
			require_once 'Joob/Http/Response/Http.php';
			$response = new Joob_Http_Response_Http();
		}
		
		try {
			$dispatcher->dispatch($request, $response);
		} catch (Exception $ex)	{
			$response->setException($ex);
		}
		
		//Release the dispatcher
		$dispatcher->destroy();
		
		require_once 'Joob/Registry/KVP.php';
		$registry = Joob_Registry_KVP::getInstance();
		$value = $registry->get("renderexception", "Joob:exceptions");
		if ($value)	{
			$response->showExceptions();
		}
		
		$response->sendResponse();
	}
}

?>